package com.bfg.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class MainController
 */
public class MainController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	HttpSession session;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MainController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String userLogin = "eko";
		String passLogin = "oke";

		String pageEntryProduct = "WEB-INF/entryproduct.jsp";
		String pageListProduct = "TestDBServlet2.do?action=insert";

		session = request.getSession();
		session.removeAttribute("loginError");
		session.removeAttribute("entryError");

		String action = request.getParameter("action");
		if (action.equals("login")) {
			String strUser = request.getParameter("username");
			String strPass = request.getParameter("password");

			if (strUser.equals(userLogin) && strPass.equals(passLogin)) {
				session.setAttribute("loginError", "n");
				goToPage(pageEntryProduct, request, response);
			} else {
				loginError(request, response);
			}
		}
		if (action.equals("submit")) {
			String str_isbn = request.getParameter("isbn");
			String str_title = request.getParameter("title");
			String str_price = request.getParameter("price");
			String str_pub_date = request.getParameter("pub_date");
			String str_description = request.getParameter("description");
			if (str_isbn.isEmpty() || str_title.isEmpty() || str_price.isEmpty()
					|| str_pub_date.isEmpty() || str_description.isEmpty()) {
				session.setAttribute("entryError", "y");
				goToPage(pageEntryProduct, request, response);

			} else {
				goToPage(pageListProduct, request, response);
			}
		}
	}

	// sending request to different page
	private void goToPage(String page, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		RequestDispatcher rd = req.getRequestDispatcher(page);
		if (rd != null)
			rd.forward(req, resp);

	}

	private void loginError(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		session.setAttribute("loginError", "y");
		goToPage("login.jsp", request, response);

	}
}
